/*
	Copyright (C) 2008 Demiurg-HG
	
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
// ui_console.cpp - 

#include "ui_local.h"

#error file should be excluded from project

//
//	EUIConsole::EUIConsole
//
EUIConsole::EUIConsole(EUserInterface *ui)
{
	this->ui	= ui;
	rs			= ui->rs;
	
	ui_con_speed = ui->shell->RegisterVar("ui_con_speed", "4", 0);
	
	is_opened	=	false;
	scroll		=	0;
	y_offset	=	0;
	con_handler	=	NULL;
}


//	
//	EUIConsole::~EUIConsole
//
EUIConsole::~EUIConsole(void)
{
}


//
//	EUIConsole::Activate
//
void EUIConsole::Activate(void)
{
	is_opened	=	true;
}


//
//	EUIConsole::Deactivate
//
void EUIConsole::Deactivate(void)
{
	is_opened	=	false;
}


//
//	EUIConsole::Draw
//
void EUIConsole::Draw(void)
{
	if (ui_con_speed->Float()<0.1) {
		ui_con_speed->SetValue("0.1");
	}
	
	if (IsOpened()) {
		y_offset += ui->core->GetDeltaTime() * ui_con_speed->Float();
	} else {
		y_offset -= ui->core->GetDeltaTime() * ui_con_speed->Float();
	}
	y_offset	=	Clamp<float>(y_offset, 0, 1);
	
	if (y_offset==0) {
		return;	// console is not visible
	}
	
	//
	//	Draw Console Stuff :
	//
	uint w, h;
	rs->GetScreenRect(w,h);
	
	float	height = (float)(3*h/4);
	float	width  = (float)(w);

	rs->DrawStretchImage(0, height*y_offset - height, width, height, "ui/con_back.tga");

	const float LINE_HEIGHT = 20;	
	const float FONT_SIZE	= 32;
	const float FONT_TRACK	= 10;
	
	float start = height * y_offset;
	int visible_line_num = (int)(start/LINE_HEIGHT);
	visible_line_num -= 1; // command line

	int line_num = ui->con->GetLineNum();

	//
	//	Command Line :
	//
	char cursor = ' ';
	if ( ui->sys->Milliseconds() & 0x100 ) cursor = '_';
	ui->DrawStringMT(0, start, FONT_SIZE, FONT_TRACK, "fonts/ocr_16.tga", va("]%s%c", command_line.c_str(), cursor));
	
	//
	//	Version Info :
	//
	string version = "^4";
	version += ui->core->Version();
	
	ui->DrawStringMTColored(w - FONT_TRACK * version.length(), start, FONT_SIZE, FONT_TRACK, "fonts/ocr_16.tga", version.c_str());


	start -= LINE_HEIGHT;
	
	//
	//	Console Text :
	//
	for (int i=0; i<visible_line_num; i++) {
		const char *line = ui->con->GetLine(line_num-i - scroll);
		
		if (line) {
			ui->DrawStringMTColored(0, start, FONT_SIZE, FONT_TRACK, "fonts/ocr_16.tga", line);
		}
		
		start -= LINE_HEIGHT;
	}
	
	// test line: console should visualize 80 chars in one line! :	
	//ui->DrawStringMT(0, start, FONT_SIZE, FONT_TRACK, "fonts/ocr_16.tga", "]......[]......[]......[]......[]......[]......[]......[]......[]......[]......[");
}


//
//	EUIConsole::KeyDown
//
void EUIConsole::KeyDown(uint ch)
{
	if (ch==KEY_BACKSPACE) {
		if (command_line.length()>0) {
			command_line.resize(command_line.length()-1);
		}
	}
	
	if (ch==KEY_PGDN) 	scroll-=2;
	if (ch==KEY_PGUP) 	scroll+=2;
	if (ch==KEY_END)	scroll=0;
	if (ch==KEY_HOME)	scroll=ui->con->GetLineNum();
	scroll = Clamp<int>(scroll, 0, ui->con->GetLineNum());
	
	if (con_handler) {
		if (ch==KEY_ENTER)	command_line = con_handler->ExecuteCommand(command_line.c_str());
		if (ch==KEY_TAB)	command_line = con_handler->CompleteCommand(command_line.c_str());
		if (ch==KEY_UP)		command_line = con_handler->PrevCommand();
		if (ch==KEY_DOWN)	command_line = con_handler->NextCommand();
	}
}

//
//	EUIConsole::KeyPress
//
void EUIConsole::KeyPress(uint ch)
{
	if (isprint(ch)) {
		command_line.push_back((char)ch);
	}
}


//
//	EUIConsole::SetConsoleHandler
//
void EUIConsole::SetInputHandler(IConsoleInputHandler *con_input_handler)
{
	con_handler	=	con_input_handler;
}







